package Memento;

import java.util.ArrayList;
import java.util.List;

public class UnRedoMemento {
    private List<Memento> undoStates;
    private List<Memento> redoStates;
    public UnRedoMemento() {
        undoStates = new ArrayList<>();
        redoStates = new ArrayList<>();
    }
    public void addMemento(Memento m) {
        redoStates.clear();
        undoStates.add(m);
    }
    public Memento undo() {
        Memento undoM=null;
        if (undoStates.size()>1){
            undoM=undoStates.get(undoStates.size()-2);
            redoStates.add(undoStates.get(undoStates.size()-1));
            undoStates.remove(undoStates.size()-1);
        }else {
            System.out.println("fail to undo operation!");
        }
        return undoM;
    }
    public Memento redo(){
        Memento undoM=null;
        if(redoStates.size()>0){
            undoM=redoStates.get(redoStates.size()-1);
            undoStates.add(undoM);
            redoStates.remove(redoStates.size()-1);
        }else {
            System.out.println("fail to redo operation!");
        }
        return undoM;
    }
}
